package com.tubang.organization.controller;


import java.sql.Timestamp;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.tubang.common.constant.Constants;
import com.tubang.common.util.GsonQuick;
import com.tubang.common.util.MsgCode;
import com.tubang.common.util.ResultData;
import com.tubang.component.entity.CompanyEntity;
import com.tubang.component.service.ICompanyService;
import com.tubang.component.util.RequestUtil;

/**
 * <p>
 * 组织表 部门  前端控制器
 * </p>
 *
 * @author maxl
 * @since 2017-11-30
 */
@RestController
@RequestMapping("/company")
public class CompanyController {
	private Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private ICompanyService companyService;


    /**
     * 获取部门列表
     * @return
     */
    @GetMapping(value = "getCompanyList")
    public ResultData getCompanyList(@RequestParam(required = false) String companyId,
                                     @RequestParam(required = false) String companyName,
                                         @RequestParam(required = false, defaultValue = "1") Integer page,
                                         @RequestParam(required = false, defaultValue = "10") Integer rows) {
        try {
            EntityWrapper<CompanyEntity> entityWrapper=new EntityWrapper<CompanyEntity>();
            if(StringUtils.isNotBlank(companyName)){
                entityWrapper.eq("name",companyName);
            }

            if(StringUtils.isNotBlank(companyId)){
                entityWrapper.eq("parent_id",companyId);
            }
            entityWrapper.eq("enable", Constants.DEFAULT_VAULE_ONE);
            Page<CompanyEntity> pageList = companyService.selectPage(new Page<CompanyEntity>(), entityWrapper);
            int count = companyService.selectCount(entityWrapper);
            return ResultData.result(true).setData(pageList.getRecords()).setTotalCount(count);
        } catch (Exception e) {
        	e.printStackTrace();
			logger.error("[getCompanyList]==> method[getCompanyList],错误原因：[{}]", e.getMessage());
            return ResultData.result(false);
        }
    }



    /**
     * 获取部门详情
     * @return
     */
    @GetMapping(value = "getCompanyInfo")
    public ResultData getCompanyInfo(@RequestParam(required = false) String companyId) {
        try {
            CompanyEntity companyEntity = companyService.selectById(companyId);
            return ResultData.result(true).setData(companyEntity);
        } catch (Exception e) {
        	e.printStackTrace();
			logger.error("[getCompanyInfo]==> method[getCompanyInfo],错误原因：[{}]", e.getMessage());
            return ResultData.result(false);
        }
    }


    /**
     * 组织 添加 更新
     * @return
     */
    @PostMapping(value = "insertOrUpdateCompanyEntity")
	public ResultData insertOrUpdateCompanyEntity(HttpServletRequest request) {
		try {
			String receivePostJson = RequestUtil.receivePostJson(request);
			CompanyEntity companyEntity = GsonQuick.toObject(receivePostJson, CompanyEntity.class);
			Timestamp now = new Timestamp(System.currentTimeMillis());
			String id = companyEntity.getId();
			if (StringUtils.isBlank(id)) {
				companyEntity.setCreateTime(now);
			}
			companyEntity.setUpdateTime(now);
			companyService.insertOrUpdate(companyEntity);
			return ResultData.result(true).setData(companyEntity);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("[insertOrUpdateCompanyUserEntity]==> method[insertOrUpdateCompanyUserEntity],错误原因：[{}]", e.getMessage());
			return ResultData.result(false);
		}
	}

    /**
     * 删除部门
     *
     * @return
     */
	@RequestMapping(value = "deleteCompanyEntity", method = RequestMethod.GET)
	public ResultData deleteCompanyEntity(@RequestParam String id) {
		try {
			CompanyEntity companyEntity = companyService.selectById(id);
			if (companyEntity == null) {
				return ResultData.result(false).setCode(MsgCode.ADDRESS_FAIL_IS_NOT_EXIST).setMsg(MsgCode.ADDRESS_FAIL_IS_NOT_EXIST_DESC);
			}
			// 更新状态
			companyEntity.setEnable(Boolean.FALSE);
			companyService.updateById(companyEntity);
			return ResultData.result(true);
		} catch (Exception e) {
			return ResultData.result(false).setMsg(MsgCode.DEFAULT_ERROR_MSG);
		}
	}
    
}
